﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace com.qbengo.utility.nodepicker.DAL
{
    /// <summary>
    /// 数据访问类:EventTimetableDao
    /// </summary>
    public partial class EventTimetableDao
    {
        public EventTimetableDao()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int recid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from event_timetable");
            strSql.Append(" where recid=@recid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@recid", MySqlDbType.Int32)
			};
            parameters[0].Value = recid;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(com.qbengo.utility.nodepicker.entity.EventTimetable model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into event_timetable(");
            strSql.Append("eventid,from_utc,to_utc,activity_name,activity_location,activity_poi)");
            strSql.Append(" values (");
            strSql.Append("@eventid,@from_utc,@to_utc,@activity_name,@activity_location,@activity_poi)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@eventid", MySqlDbType.Int32,11),
					new MySqlParameter("@from_utc", MySqlDbType.DateTime),
					new MySqlParameter("@to_utc", MySqlDbType.DateTime),
					new MySqlParameter("@activity_name", MySqlDbType.Text),
					new MySqlParameter("@activity_location", MySqlDbType.Text),
					new MySqlParameter("@activity_poi", MySqlDbType.Int32,11)};
            parameters[0].Value = model.eventid;
            parameters[1].Value = model.from_utc;
            parameters[2].Value = model.to_utc;
            parameters[3].Value = model.activity_name;
            parameters[4].Value = model.activity_location;
            parameters[5].Value = model.activity_poi;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(com.qbengo.utility.nodepicker.entity.EventTimetable model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update event_timetable set ");
            strSql.Append("eventid=@eventid,");
            strSql.Append("from_utc=@from_utc,");
            strSql.Append("to_utc=@to_utc,");
            strSql.Append("activity_name=@activity_name,");
            strSql.Append("activity_location=@activity_location,");
            strSql.Append("activity_poi=@activity_poi");
            strSql.Append(" where recid=@recid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@eventid", MySqlDbType.Int32,11),
					new MySqlParameter("@from_utc", MySqlDbType.DateTime),
					new MySqlParameter("@to_utc", MySqlDbType.DateTime),
					new MySqlParameter("@activity_name", MySqlDbType.Text),
					new MySqlParameter("@activity_location", MySqlDbType.Text),
					new MySqlParameter("@activity_poi", MySqlDbType.Int32,11),
					new MySqlParameter("@recid", MySqlDbType.Int32,11)};
            parameters[0].Value = model.eventid;
            parameters[1].Value = model.from_utc;
            parameters[2].Value = model.to_utc;
            parameters[3].Value = model.activity_name;
            parameters[4].Value = model.activity_location;
            parameters[5].Value = model.activity_poi;
            parameters[6].Value = model.recid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int recid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from event_timetable ");
            strSql.Append(" where recid=@recid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@recid", MySqlDbType.Int32)
			};
            parameters[0].Value = recid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string recidlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from event_timetable ");
            strSql.Append(" where recid in (" + recidlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public com.qbengo.utility.nodepicker.entity.EventTimetable GetModel(int recid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select recid,eventid,from_utc,to_utc,activity_name,activity_location,activity_poi from event_timetable ");
            strSql.Append(" where recid=@recid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@recid", MySqlDbType.Int32)
			};
            parameters[0].Value = recid;

            com.qbengo.utility.nodepicker.entity.EventTimetable model = new com.qbengo.utility.nodepicker.entity.EventTimetable();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["recid"] != null && ds.Tables[0].Rows[0]["recid"].ToString() != "")
                {
                    model.recid = int.Parse(ds.Tables[0].Rows[0]["recid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["eventid"] != null && ds.Tables[0].Rows[0]["eventid"].ToString() != "")
                {
                    model.eventid = int.Parse(ds.Tables[0].Rows[0]["eventid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["from_utc"] != null && ds.Tables[0].Rows[0]["from_utc"].ToString() != "")
                {
                    model.from_utc = DateTime.Parse(ds.Tables[0].Rows[0]["from_utc"].ToString());
                }
                if (ds.Tables[0].Rows[0]["to_utc"] != null && ds.Tables[0].Rows[0]["to_utc"].ToString() != "")
                {
                    model.to_utc = DateTime.Parse(ds.Tables[0].Rows[0]["to_utc"].ToString());
                }
                if (ds.Tables[0].Rows[0]["activity_name"] != null && ds.Tables[0].Rows[0]["activity_name"].ToString() != "")
                {
                    model.activity_name = ds.Tables[0].Rows[0]["activity_name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["activity_location"] != null && ds.Tables[0].Rows[0]["activity_location"].ToString() != "")
                {
                    model.activity_location = ds.Tables[0].Rows[0]["activity_location"].ToString();
                }
                if (ds.Tables[0].Rows[0]["activity_poi"] != null && ds.Tables[0].Rows[0]["activity_poi"].ToString() != "")
                {
                    model.activity_poi = int.Parse(ds.Tables[0].Rows[0]["activity_poi"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select recid,eventid,from_utc,to_utc,activity_name,activity_location,activity_poi ");
            strSql.Append(" FROM event_timetable ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM event_timetable ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.recid desc");
            }
            strSql.Append(")AS Row, T.*  from event_timetable T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "event_timetable";
            parameters[1].Value = "recid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

